﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Utils;
using Ninject;
using Mabalacat.CPS.BusinessLogic.Services.Interfaces;
using Mabalacat.CPS.DataAccess.DataObjects;

namespace Web.Admin
{
    public partial class EmployeeEntry : System.Web.UI.Page
    {
        [Inject]
        public IEmployeeService _employeeService { get; set; }

        public bool IsSuccess { get; set; }
        public string NotifyText { get; set; }
        private int _id;
        public int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        #region Implementations

        void Edit(int id)
        {
            var employee = _employeeService.GetById(id);
            if (employee == null)
                return;
            ObjectToData(employee);

            EmployeeIdhf.Value = id.ToString();
            SubmitBtn.Text = "Update & ClearForm";
            SubmitBtn2.Text = "Update";
        }

        void Update(int id)
        {
            var employee = _employeeService.GetById(id);
            if (employee == null)
            {
                Add();
                return;
            }
            DataToObject(ref employee);

            _employeeService.Update(employee);
            IsSuccess = _employeeService.Save();
            NotifyText = "Successfully Updated Record.";                        
        }
        void Add()
        {
            var employee = new Employee();

            DataToObject(ref employee);

            _employeeService.Add(employee);
            IsSuccess = _employeeService.Save();

            EmployeeIdhf.Value = employee.Id.ToString();
            NotifyText = "Successfully Added New Record.";
            SubmitBtn.Text = "Update & ClearForm";
            SubmitBtn2.Text = "Update";
           
        }
        void ClearForm()
        {
            ClearControl(this.Page);
            EmployeeIdhf.Value = "0";
            SubmitBtn.Text = "Submit & ClearForm";
            SubmitBtn2.Text = "Submit";
        }
        void ClearControl(Control ctrl)
        {
            foreach (Control _ctrl in ctrl.Controls)
            {
                if(_ctrl.GetType() == typeof(TextBox))
                {
                    ((TextBox)_ctrl).Text = string.Empty;
                }
                else if (_ctrl.GetType() == typeof(DropDownList))
                {
                    ((DropDownList)_ctrl).SelectedIndex = 0;
                }
                ClearControl(_ctrl);
            }    
        }
        void Save()
        {
            int empId;
            int.TryParse(EmployeeIdhf.Value, out empId);
            if (empId == 0)
                Add();
            else
                Update(empId);
        }
        void DataToObject(ref Employee obj)
        {
            var dob = new DateTime(int.Parse(YearDroplist.SelectedValue), 
                int.Parse(MonthDroplist.SelectedValue), 
                int.Parse(DayDroplist.SelectedValue));

            obj.FirstName = FirstNameTxt.Text;
            obj.LastName = LastNameTxt.Text;
            obj.MiddleName = MidNameTxt.Text;
            obj.DateOfBirth = dob;
            obj.Gender = SexDropList.SelectedValue.ToString();
            obj.CivilStatus = CivilStatDropList.SelectedValue.ToString();
            obj.ContactNo = ContactTxt.Text;
            obj.Address = AddressTxt.Text;
            obj.Remarks = RemarksTxt.Text ;

            //account
            obj.TaxIDNo = TaxIdTxt.Text;
            obj.GSISPolicyNo = GSISPolicyTxt.Text;
            obj.GSISCardNo = GSISCardNoTxt.Text;
            obj.PagIbigIDNo = PagibigIdNoTxt.Text;
            obj.PhilHealthIDNo = PhilHealthIdNoTxt.Text;

            //employment
            if (PositionDropList.SelectedIndex > 0)
                obj.PositionId = int.Parse(PositionDropList.SelectedValue);
            if (DivisionDropList.SelectedIndex > 0)
                obj.DivisionId = int.Parse(DivisionDropList.SelectedValue);

            obj.SalaryGrade = SalaryGradeTxt.Text;
            obj.SalaryStep = SalaryStepTxt.Text;

        }

        void ObjectToData(Employee obj)
        {
            FirstNameTxt.Text = obj.FirstName;
            LastNameTxt.Text = obj.LastName;
            MidNameTxt.Text = obj.MiddleName;
            if (obj.DateOfBirth.HasValue)
            {
                YearDroplist.SelectedValue = obj.DateOfBirth.Value.Year.ToString();
                MonthDroplist.SelectedValue = obj.DateOfBirth.Value.Month.ToString();
                DayDroplist.SelectedValue = obj.DateOfBirth.Value.Day.ToString().PadLeft(2,'0');                
            }

            var gender = SexDropList.Items.FindByValue(obj.Gender);
            if (gender != null)
                gender.Selected = true;

            var civil = CivilStatDropList.Items.FindByValue(obj.CivilStatus);
            if (civil != null)
                civil.Selected = true;
            
            
            ContactTxt.Text = obj.ContactNo ;
            AddressTxt.Text = obj.Address;
            RemarksTxt.Text = obj.Remarks;

            //account
            TaxIdTxt.Text = obj.TaxIDNo;
            GSISPolicyTxt.Text =  obj.GSISPolicyNo ;
            GSISCardNoTxt.Text =  obj.GSISCardNo ;
            PagibigIdNoTxt.Text  = obj.PagIbigIDNo ;
            PhilHealthIdNoTxt.Text  = obj.PhilHealthIDNo;

            //employment
            if (obj.PositionId.HasValue)
            {
                var pos = PositionDropList.Items.FindByValue(obj.PositionId.Value.ToString());
                if (pos != null)
                    pos.Selected = true;            
            }
            if (obj.DivisionId.HasValue)
            {
                var div = DivisionDropList.Items.FindByValue(obj.DivisionId.Value.ToString());
                if (div != null)
                    div.Selected = true;
            }

            SalaryGradeTxt.Text = obj.SalaryGrade;
            SalaryStepTxt.Text = obj.SalaryStep;

        }
        #endregion

        protected override void OnInit(EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                int.TryParse((string)Request.QueryString["id"], out _id);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                DropDownHelper.ConstructDateDropdowns(MonthDroplist, DayDroplist, YearDroplist, null);                
                PositionDropList.ToPositionSelectList(0);
                DivisionDropList.ToDivisionSelectList(0);

                if (Id > 0)
                    Edit(Id);
            }
        }

        protected void SubmitBtn_Click(object sender, EventArgs e)
        {
            Save();            
            Response.Redirect(Request.Path);
        }
        protected void SubmitBtn2_Click(object sender, EventArgs e)
        {
            Save();            
        }
        protected void CancelBtn_Click(object sender, EventArgs e)
        {
            ClearForm();
        }
    }
}